# -*- coding=utf8 -*-#
from django.db import models
from core.models.user import User
from core.models.subject import Subject
from core.models.groups import Group
from chtozadali.core.views.utilits import cut_text

class Task(models.Model):
    text = models.TextField()
    from_date = models.DateField()
    to_date = models.DateField()
    subject = models.ForeignKey(Subject)
    title = models.CharField(max_length=100, blank=True)
    task_type = models.CharField(max_length=100, blank=True)
    #author = models.ForeignKey(User) raises ValidationError
    user = models.ManyToManyField(User, through="UserTask")
    group = models.ManyToManyField(Group)

    def __unicode__(self):
        if self.title:
            return " - ".join([unicode(self.id), unicode(self.title)])
        else:
            return " - ".join([unicode(self.id), cut_text(self.text, 100)])

    class Meta:
        app_label = 'core'

class UserTask(models.Model):
    user = models.ForeignKey(User)
    task = models.ForeignKey(Task)
    is_done = models.BooleanField()
    private_note = models.TextField(blank=True)
    priority = models.IntegerField()

    class Meta:
        app_label = "core"

    def __unicode__(self):
        return " - ".join([unicode(self.user), unicode(self.priority), unicode(self.task), unicode(self.is_done)])
